package com.bank.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bank.dao.*;
import com.bank.entity.Account;
import com.bank.entity.Subject;

@Service
public class AccountService   {

	Logger logger = Logger.getLogger(AccountService.class);
	
	private final String SUBJECT_OPEN_ACCOUT="100102";
	
	@Autowired
	AccountDao accountDao;
	
	@Autowired
	BaseDao baseDao;
	
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public  void openAccount(Account account,HttpServletRequest request)
	{
		logger.debug("开户交易开始");
		logger.debug("account"+account.getAccounting_ori());
		logger.debug("account"+account.getSenior_oir());
		logger.debug("customer_number"+account.getCustomer_number());
		Subject subject = baseDao.getSubjectByNumer(SUBJECT_OPEN_ACCOUT);
		String  number = account.getSenior_oir()+account.getAccounting_ori()+account.getCurrency()+SUBJECT_OPEN_ACCOUT+accountDao.getAccountSerno();
		account.setNumber(number);
		account.setAccount_subject(SUBJECT_OPEN_ACCOUT);
		account.setBalance_direct(subject.getSubject_balance_direct());
		account.setBalance(0.00);
		account.setBalance_pre(0.00);
		account.setProduct_number(0.00);
		account.setAccount_status("1");			//1-正常  
		accountDao.save(account);
	}
	
	//更新账户状态
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public void  updateAccount(String number){
		accountDao.updateAccountStatus(number);
	}
	
	/**
	 * 查询账户信息
	 * 
	 * **/
	public Account getAccount(String number){
		return accountDao.getAccountByNumber(number);
	}
	
	
	public List<Object> getAccountList(Map<String, Object> params){
		
		return accountDao.getAcountList(params);
	}
	
	public int getAccountListCount(Map<String, Object> params){
		return accountDao.getAccountListCount(params);
	}
	
	/**
	 * 账户验证
	 * @param   number 账号
	 * @return  验证结果
	 * **/
	public boolean proveAccount(String number, String password){
		
		Account account = this.getAccount(number);
		if(account == null)
			return false;
		return account.getPassword().equals(password);
	}
	/**
	 * 获取内部账
	 * 
	 * */
	public Account  getAccountByParams(Map<String, Object> params){
		
		String currency = (String)params.get("currency");//币种
		String accounting_ori = (String)params.get("accounting_ori");//帐务机构
		String  account_subject =(String)params.get("account_subject");//科目号
		
		return accountDao.getAccountByParams(currency, accounting_ori, account_subject);
	}
	
	
}
